﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CinemaLib;
using OnlineCinemaBooking.BookingService;

namespace OnlineCinemaBooking
{
    public partial class UserInfo : System.Web.UI.Page
    {
        Reservation reservation;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                reservation = (Reservation)Session["ReservationData"];
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (InvalidCastException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        protected void ButtonProceed_Click(object sender, EventArgs e)
        {
            try
            {
                using (BookingService.BookingServiceClient proxy = new BookingServiceClient())
                {
                    proxy.Open();
                    if (CheckBoxRegister.Checked)
                    {
                        reservation.BookedUser = new User(TextBoxFirstName.Text, TextBoxLastName.Text, int.Parse(TextBoxPhone.Text), TextBoxEmail.Text, TextBoxPassword.Text);
                    }
                    else
                    {
                        reservation.BookedUser = new User(TextBoxFirstName.Text, TextBoxLastName.Text, int.Parse(TextBoxPhone.Text));
                    }
                    proxy.Order(reservation);
                }
                Response.Redirect("Completed.aspx", false);
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (TimeoutException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                using (BookingService.BookingServiceClient proxy = new BookingServiceClient())
                {
                    proxy.Open();
                    reservation.BookedUser = proxy.VerifyUser(TextBoxLoginEmail.Text, TextBoxLoginPassword.Text);
                    proxy.Order(reservation);
                }
                Response.Redirect("Completed.aspx");
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (TimeoutException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            try
            {
                using (BookingService.BookingServiceClient proxy = new BookingServiceClient())
                {
                    proxy.Open();
                    proxy.Cancel(((Reservation)Session["ReservationData"]).ID.ToString());
                }
                Session.Abandon();
                Response.Redirect("Default.aspx");
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (TimeoutException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }
    }
}